package com.jubaopen.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jubaopen.bean.AuthInfo;
import com.jubaopen.dao.AuthInfoDAO;
import com.jubaopen.util.PagedList;

public class AuthInfoService {

	private AuthInfoDAO authInfoDAO;

	public void setAuthInfoDAO(AuthInfoDAO authInfoDAO) {
		this.authInfoDAO = authInfoDAO;
	}
	
	public boolean save(AuthInfo authInfo){
		AuthInfo entry = authInfoDAO.getByUserId(authInfo.getUserId());
		int ret = 0;
		
		if(entry != null){
			authInfo.setId(entry.getId());
			ret = authInfoDAO.update(authInfo);
		}else {
			ret = authInfoDAO.add(authInfo);
		}
		
		return ret>0 ? true:false; 
	}
	
	public AuthInfo queryByUserId(int userId){
		return authInfoDAO.getByUserId(userId);
	}
	
	public AuthInfo queryById(int id){
		return authInfoDAO.getById(id);
	}
	
	public PagedList<AuthInfo> getAuthList(Map<String, Object> queryMap,
			int pageIndex, int pageSize) {
		int skip = pageIndex * pageSize;
		List<AuthInfo> authInfoList = new ArrayList<AuthInfo>();
		queryMap.put("skip", skip);
		queryMap.put("pageSize", pageSize);
		try {
			authInfoList = authInfoDAO.getPagedList(queryMap);
			PagedList<AuthInfo> ret = new PagedList<AuthInfo>();
			ret.setList(authInfoList);
			if (pageIndex == 0) {
				int count = authInfoDAO.getCount(queryMap);
				ret.setCount(count);
			}
			return ret;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
